package com.rjgc.title.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rjgc.title.common.util.MD5;
import com.rjgc.title.mapper.UserInfoMapper;
import com.rjgc.title.model.entity.user.UserInfo;
import com.rjgc.title.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @program: title-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-24 23:08
 **/
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public UserInfo getUserInfo(String userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        return userInfo;
    }

    /**
     *  // 修改密码
     * @param userInfo
     * @param oPassword
     * @param nPassword
     * @return
     */
    @Override
    public Boolean updatePass(UserInfo userInfo, String oPassword, String nPassword) {

        if (oPassword.equals(userInfo.getPasswd())) {
            userInfo.setPasswd(nPassword);

            // 更细数据
            userInfoMapper.updateById(userInfo);
            return true;
        }

        return false;
    }

    /**
     * 保存或者修改数据
     *
     * @param userInfo
     */
    @Override
    public void saveUserInfo(UserInfo userInfo) {
        // 判断当前操作是添加或者是修改

        if (userInfo.getId() != null) {
            //  修改
            userInfoMapper.updateById(userInfo);
        } else {
            // 增加
            userInfoMapper.insert(userInfo);
        }

    }

    /**
     * 查找详细用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public UserInfo findUserInfo(String userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        return userInfo;
    }


    /**
     * 登录功能
     *
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo login(UserInfo userInfo) {
        // 获取登录名字
        String loginName = userInfo.getLoginName();

        // 进行密码加密   拓展我们可以加盐
        String encrypt = MD5.encrypt(userInfo.getPasswd());

        // 创建查询条件
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", loginName);
        queryWrapper.eq("passwd", encrypt);

        UserInfo selectOne = userInfoMapper.selectOne(queryWrapper);
        // 返回数据
        return selectOne;
    }
}
